
package org.mbds.wolf.db;

import org.mbds.wolf.exception.UserException;
import org.mbds.wolf.exception.UserExceptionReason;

/**
 *
 * @author amles_000
 */
public class DataTableUser extends DataTable {
    
    public final static byte[] COL_LOGIN_NAME = {'L','O','G','I','N'};
    public final static byte[] COL_PASSWORD_NAME = {'P','A','S','S','W','O','R','D'};
    public final static byte[] COL_PRIVILEGES_NAME = {'P','R','I','V','I','L','E','G','E','S'};
    public final static short COL_LOGIN_ID = 0x00;
    public final static short COL_PASSWORD_ID = 0x01;
    public final static short COL_PRIVILEGE_ID = 0x02;
    
    /**
     * Constructor
     * @param id
     * @param name 
     */
    public DataTableUser(short id, byte[] name) {
        super(id, name);
        createColumns();
    }
    
    /**
     * insert
     * @param columnName
     * @param data
     * @return id or -1 if column not found
     * @throws UserException 
     */
    @Override
    short insert(byte[] columnName, byte[] data) throws UserException {
        if (DataTool.equals(columnName, COL_LOGIN_NAME)) {
           if (getUserId(data)>=0)
               UserException.throwIt(UserExceptionReason.DB_ATOMIC_DATA_NAME_ALREADY_USED);
        }
        return super.insert(columnName, data);
    }
    /**
     * insert
     * @param columnId
     * @param data
     * @return id or -1 if column not found
     * @throws UserException 
     */
    @Override
    short insert(short columnId, byte[] data) throws UserException {
        if (columnId==COL_LOGIN_ID) {
           if (getUserId(data)>=0)
               UserException.throwIt(UserExceptionReason.DB_ATOMIC_DATA_NAME_ALREADY_USED);
        }
        return super.insert(columnId, data);
    }    
    /**
     * insert
     * @param columnId
     * @param dataArray
     * @return -1
     * @throws UserException 
     */
    @Override
    short insert(short columnId, Object[] dataArray) throws UserException {
        UserException.throwIt(UserExceptionReason.DB_UNHAUTORIZED_INSTRUCTION_EXCEPTION);
        return -1;
    }
    /**
     * insert
     * @param columnName
     * @param dataArray
     * @return -1
     * @throws UserException 
     */
    @Override
    short insert(byte[] columnName, Object[] dataArray) throws UserException {
        UserException.throwIt(UserExceptionReason.DB_UNHAUTORIZED_INSTRUCTION_EXCEPTION);
        return -1;
    }
    
    /**
     * checkUser
     * @param login
     * @param password
     * @return boolean
     */
    boolean checkUser(byte[] login, byte[] password) {
        try {
            short index = getUserId(login);
            if (index == -1) {
                return false;
            }
            byte[] pwd = select(COL_PASSWORD_ID, index);
            if (DataTool.equals(password, pwd)) {
                return true;
            }
        } catch (UserException e) {
            //return false;
        }
        return false;
    }
    
    /**
     * searchUser
     * @param login
     * @return userID
     * @throws UserException 
     */
    short getUserId(byte[] login) throws UserException {
        AtomicData[] users = select(DataTableUser.COL_LOGIN_ID);
        return DataTool.containsValue(login, users); 
    }
    
    /**
     * checkUserPrivilege
     * @param login
     * @param privilegeId
     * @return boolean
     */
    boolean checkUserPrivilege(byte[] login, short privilegeId) {
        AtomicData[] users = select(DataTableUser.COL_LOGIN_ID);
        try {
            short index = DataTool.containsValue(login, users);
            if (index == -1) 
                return false;
            byte[] lst = select(DataTableUser.COL_PRIVILEGE_ID, index);
            if (lst==null)
                return false;
            for (short i=0;i<lst.length;i++)
                if (lst[i]==privilegeId)
                    return true;
        } catch (UserException e) {
            //return false;
        }
        return false;
    }

    /**
     * addColumn
     * @param id
     * @param name
     * @throws UserException 
     */
    @Override
    void addColumn(final short id, final byte[] name) throws UserException {
        UserException.throwIt(UserExceptionReason.DB_UNHAUTORIZED_INSTRUCTION_EXCEPTION);
    }    
   
    /**
     * renameColumn
     * @param new column name 
     * @throws UserException 
     */
    @Override
    void renameColumn(short id, byte[] newName) throws UserException {
        UserException.throwIt(UserExceptionReason.DB_UNHAUTORIZED_INSTRUCTION_EXCEPTION);
    }   
    
    /**
     * drop
     * @return boolean
     */
    @Override
    boolean drop() {
        return false;
    }
    
    /**
     * rename
     * @param new column name 
     * @throws UserException 
     */
    @Override
    void rename(byte[] name) throws UserException {
        UserException.throwIt(UserExceptionReason.DB_UNHAUTORIZED_INSTRUCTION_EXCEPTION);
    }
    
    /**
     * dropColumnById
     * @param id
     * @return false
     */
    @Override
    boolean dropColumnById(short id) {
        return false;
    }
    
    /**
     * dropColumnByName
     * @param name
     * @return false
     */
    @Override 
    boolean dropColumnByName(byte[] name) {
        return false;
    }

        /**
     * update
     * @param columnId
     * @param recordId
     * @param newValue
     * @return nb records updated
     */
    @Override
    short updateData(short columnId, short recordId, byte[] newValue) {
        if (columnId == COL_LOGIN_ID)
            return 0;
        return super.updateData(columnId, recordId, newValue);    
    }    
    
    /**
     * update
     * @param columnName
     * @param recordId
     * @param newValue
     * @return nb records updated
     * @throws UserException 
     */
    @Override
    short updateData(byte[] columnName, short recordId, byte[] newValue) throws UserException {
        if (DataTool.equals(columnName, COL_LOGIN_NAME))
            UserException.throwIt(UserExceptionReason.DB_UNHAUTORIZED_INSTRUCTION_EXCEPTION);
        return super.updateData(columnName, recordId, newValue);    
    }
    
    /**
     * createColumns
     */
    private void createColumns() {
        try {
            super.addColumn(COL_LOGIN_ID, COL_LOGIN_NAME);
        } catch (UserException ex) {
        }
        try {
            super.addColumn(COL_PASSWORD_ID, COL_PASSWORD_NAME);
        } catch (UserException ex) {
        }
        try {
            super.addColumn(COL_PRIVILEGE_ID, COL_PRIVILEGES_NAME);
        } catch (UserException ex) {
        }
    }   
}